package com.smart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.common.PageInfo;
import com.smart.model.dtomapper.ProjectItemDtoMapper;
import com.smart.model.domain.ProjectItemDO;
import com.smart.mapper.ProjectItemMapper;
import com.smart.model.request.ProjectItemRequest;
import com.smart.model.request.ProjectWeeklyItemPageRequest;
import com.smart.service.ProjectItemService;
import com.smart.model.convert.ProjectItemConvert;
import com.smart.model.vo.ProjectItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class ProjectItemServiceImpl extends ServiceImpl<ProjectItemMapper, ProjectItemDO> implements ProjectItemService {
    @Autowired
    private ProjectItemMapper projectItemMapper;

    @Override
    public ProjectItemVO getByKey(long id) {
        ProjectItemDO projectItemDO = getById(id);
        ProjectItemVO projectItemVO = ProjectItemDtoMapper.INSTANCE.do2VO(projectItemDO);
        return projectItemVO;
    }

    @Override
    public PageInfo<ProjectItemVO> pageQuery(ProjectItemRequest request) {
        long count = projectItemMapper.pageCount(request);
        List<ProjectItemDO> list = projectItemMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<ProjectItemVO> projectItemVOList = ProjectItemConvert.buildProjectItemVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), projectItemVOList, count);
    }

    @Override
    public PageInfo<ProjectItemVO> pageQuery(ProjectWeeklyItemPageRequest request) {
        long count = projectItemMapper.pageItemCount(request);
        List<ProjectItemDO> list = projectItemMapper.pageItemQuery(request, request.getPageSize(), request.fetchOffSet());
        List<ProjectItemVO> projectItemVOList = ProjectItemConvert.buildProjectItemVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), projectItemVOList, count);
    }

    @Override
    public List<ProjectItemVO> listQuery(ProjectItemRequest request) {
        List<ProjectItemDO> list = list();
        List<ProjectItemVO> projectItemVOS = ProjectItemConvert.buildProjectItemVoList(list);
        return projectItemVOS;
    }

    @Override
    public Boolean add(ProjectItemRequest request) {
        ProjectItemDO projectItemDO = ProjectItemDtoMapper.INSTANCE.request2DO(request);
        save(projectItemDO);
        return true;
    }

    @Override
    public Boolean updateByKey(ProjectItemRequest request) {
        ProjectItemDO projectItemDO = ProjectItemDtoMapper.INSTANCE.request2DO(request);
        save(projectItemDO);
        return true;
    }

    @Override
    public Boolean deleteByKey(long id) {
        projectItemMapper.deleteDOById(id);
        return true;
    }

    @Override
    public List<ProjectItemDO> listByTaskId(Long taskId) {
        return projectItemMapper.listByTaskId(taskId);
    }
}
